<?php

/**

 * @see Alinks_Ajax_Callable_Interface

 */

require_once('Alinks/Ajax/Callable/Interface.php');



/**

 * Handles the export button being clicked on the keyphrases

 * page

 *

 * Copyright (C) 2008  Sean Hickey

 * 

 * This program is free software: you can redistribute it and/or modify

 * it under the terms of the GNU General Public License as published by

 * the Free Software Foundation, either version 3 of the License, or

 * (at your option) any later version.

 *

 * This program is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

 * GNU General Public License for more details.

 *

 * You should have received a copy of the GNU General Public License

 * along with this program.  If not, see <http://www.gnu.org/licenses/>.

 *

 * @author Sean Hickey <sean@headzoo.com>

 * @version $Id: Ajax.php 390 2008-07-03 05:40:51Z sean $

 * @package Alinks_ImportExport

 */

class Alinks_ImportExport_Export_Ajax implements Alinks_Ajax_Callable_Interface

{

	/**

	 * Whether the next method call is coming from an Ajax

	 * request

	 *

	 * @var bool

	 */

	protected $_ajax_calling = false;

	

	/**

	 * Handles the export button being clicked on the keyphrases page

	 *

	 * Here's the deal: When the export button is clicked on the keyphrases

	 * menu, the hashes for the clicked keyphrases are send back to the server

	 * using an Ajax request. The server then exports those keyphrases, but

	 * saves the document to the temp directory.

	 * The backend replies to the Ajax request with a URL that it should 

	 * redirect the user to via window.location. The URL will contain the

	 * name of the file written to the temp directory, along with the file's

	 * mime type, and file extension.

	 * This method intercepts that URL request. It gets the file name, mime

	 * type, and file extension from the URL, and then sends the file to

	 * the user.

	 * If there's a way to do this all in JavaScript, then I'm all ears.

	 */

	public static function init()

	{

		if (isset($_GET['alinks_download']) && isset($_GET['alinks_mime']) && isset($_GET['alinks_ext'])) {

			check_ajax_referer();

			if (!is_user_logged_in()) {

				return;

			}

			

			/**

			 * This should only work for logged in admins

			 */

			$current = wp_get_current_user();

			$current = get_userdata($current->ID);

			if ($current->user_level < 10) {

				return;

			}

			if (!file_exists(Alinks_Registry::get('temp_path') . '/' . $_GET['alinks_download'])) {

				return;

			}

			

			$data = file_get_contents(Alinks_Registry::get('temp_path') . '/' . $_GET['alinks_download']);

			@unlink(Alinks_Registry::get('temp_path') . '/' . $_GET['alinks_download']);

			header('Content-type: ' . $_GET['alinks_mime']);

			header('Content-Disposition: attachment; filename="alinks-' . date('Y-m-d-H-i-s') . '.' . $_GET['alinks_ext'] . '"');

			echo $data;

			die();

		}

	}

	

	/**

	 * Implements Alinks_Ajax_Callable_Interface::setAjaxCalling

	 */

	public function setAjaxCalling()

	{

		$this->_ajax_calling = true;

	}

	

	/**

	 * Exports keyphrases from an Ajax request

	 *

	 * @param array $hashes The hashes for the keyphrases to export

	 */

	public function exportKeyphrases($hashes)

	{

		/**

		 * @see Alinks_ImportExport_Export_Factory

		 */

		require_once('Alinks/ImportExport/Export/Factory.php');

		$exporter = Alinks_ImportExport_Export_Factory::factory(Alinks_Settings::get('alinks', Alinks_Settings::DEFAULT_EXPORT, Alinks_Settings::DEFAULT_DEFAULT_EXPORT));

		$exporter->addOption(Alinks_ImportExport_Export_Abstract::EXPORT_HASHES, $hashes);

		$data = $exporter->export();

		

		$file = 'alinks-' . date('Y-m-d-H-i-s') . '.' . $exporter->getFileExt();

		file_put_contents(Alinks_Registry::get('temp_path') . '/' . $file, $data);

		

		Alinks_Ajax::sendResponse(true, null, array(

			'file_url' => Alinks_Settings::get('alinks', 'home')

			. '/?alinks_mime=' . urlencode($exporter->getMimeType())

			. '&alinks_ext=' . urlencode($exporter->getFileExt())

			. '&alinks_download=' . urlencode($file)

		));

	}

}

